﻿/* 2010/6/1 */
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.IO;
using System.Diagnostics;
using Cosmos.Windows.Themes;

namespace PictureShower
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string argString = null;
            foreach (string arg in args)
            {
                argString += String.Concat(arg, "\n");
            }
            
            if (args.Length > 0)
            {
                string arg_op = args[0].ToLower(CultureInfo.InvariantCulture).Trim().Substring(0, 2);

                switch (arg_op)
                {
                    case "/c":
                        // 显示选项对话框
                        ShowOptions();
                        break;
                    case "/p":
                        // 进行预览操作
                        IntPtr previewHandle = IntPtr.Zero;
                        if (args.Length > 1)
                            previewHandle = (IntPtr)Int32.Parse(args[1]);
                        ShowPreview(previewHandle);
                        break;
                    case "/s":
                        // 显示屏幕保护程序窗体，区分测试运行与实际运行，/S为测试运行，/s为实际运行。
                        if (args[0].Equals("/S"))
                            ShowScreenSaver(true);
                        else
                            ShowScreenSaver(false);
                        break;
                    default:
                        MessageBox.Show("无效的命令行参数 :\"" + arg_op + "\"", "无效的命令行参数", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                }
            }
            else
            {
                // 如果未传入参数，则显示屏幕保护程序
                ShowOptions();
            }
        }

        /// <summary>
        /// 显示设置窗口。
        /// </summary>
        static void ShowOptions()
        {
            try
            {
                Application.Run(new OptionsForm());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 显示预览。
        /// </summary>
        /// <param name="previewHandle">预览窗口句柄。</param>
        static void ShowPreview(IntPtr previewHandle)
        {
            try
            {
                ThemeForm aForm = new ThemeForm(new Size(170, 150));

                if (ThemeApiProvider.SetParent(aForm.Handle, previewHandle) == IntPtr.Zero)
                {
                    MessageBox.Show("预览出错");
                    return;
                }
                Application.Run(aForm);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 显示屏保。根据是否为测试运行决定是否禁用系统屏保。
        /// </summary>
        /// <param name="isTest">是否为测试运行。</param>
        static void ShowScreenSaver(bool isTest)
        {
            try
            {
                if (isTest)
                    ThemeApiProvider.DisableSystemScrennSaver();
                Application.Run(new ThemeForm());
                if (isTest)
                    ThemeApiProvider.EnableSystemScreenSaver();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }
    }
}